package com.yeu.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.yeu.util.ConnectionPool;
public class StatisticsAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String eventType;
	private String year;
	private String month;
	private String department;
	private Map<String, Boolean> boolResult;
	private Map<String,Object> mapResult;
	private List<Map<String,String>> result;
	private int rows;
	private int page;
	private String sort;
	private String order;
	
	public Map<String, Boolean> getBoolResult() {
		return boolResult;
	}
	public void setBoolResult(Map<String, Boolean> boolResult) {
		this.boolResult = boolResult;
	}
	public Map<String, Object> getMapResult() {
		return mapResult;
	}
	public void setMapResult(Map<String, Object> mapResult) {
		this.mapResult = mapResult;
	}
	public List<Map<String, String>> getResult() {
		return result;
	}
	public void setResult(List<Map<String, String>> result) {
		this.result = result;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getEventType() {
		return eventType;
	}
	public void setEventType(String eventType) {
		this.eventType = eventType;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	
	public String query(){
		List<Map<String,String>> tempResult = new ArrayList<Map<String,String>>();
		result = new ArrayList<Map<String,String>>();
		Map<String,String> map = new HashMap<String,String>();
		String filter = "where ";
		if(!department.equals(""))
			filter = filter + "ADMINDEPARTMENT='"+department + "' and ";
		if(!year.equals(""))
			filter = filter  + "SUBSTR(EVENTDATE,0,4)='"+year + "' and ";
		if(!month.equals(""))
			filter = filter + "SUBSTR(EVENTDATE,6,2)='"+month + "' and ";
		if(!eventType.equals(""))
			filter = filter + "EVENTTYPE='"+eventType + "' and " ;
		if(filter.equals("where ")){
			filter = "";
		}
		else{
			int length = filter.length();
			filter = filter.substring(0,length-5);
		}
		System.out.println("filter="+filter);
		String sqlTotal = "select COUNT(*) from EVENT2 " + filter;
		tempResult = ConnectionPool.executeQuery(sqlTotal);
		map.put("TOTAL", tempResult.get(0).get("COUNT(*)"));
		String sqlUnfinished = "";
		if(filter.equals(""))
			sqlUnfinished = "select COUNT(*) from EVENT2 " + " where EVENTSTATUS = " + "'未完成'";
		else
			sqlUnfinished = "select COUNT(*) from EVENT2 " + filter + " and EVENTSTATUS = " + "'未完成'";
		tempResult = ConnectionPool.executeQuery(sqlUnfinished);
		map.put("UNFINISHED", tempResult.get(0).get("COUNT(*)"));
		String sqlFinished = "";
		if(filter.equals(""))
			sqlFinished = "select COUNT(*) from EVENT2 " + " where EVENTSTATUS = " + "'已完成'";
		else
			sqlFinished = "select COUNT(*) from EVENT2 " + filter + " and EVENTSTATUS = " + "'已完成'";
		tempResult = ConnectionPool.executeQuery(sqlFinished);
		map.put("FINISHED", tempResult.get(0).get("COUNT(*)"));
		double percentage = Double.parseDouble(map.get("FINISHED")) / Double.parseDouble(map.get("TOTAL"));
		int percent = (int)(percentage * 100);
		map.put("PERCENT", Integer.toString(percent));
		result.add(map);
		return "success";
	}

}
